package com.spsoft.vms.redis.service.impl;

import com.spsoft.vms.redis.constant.RedisConstant;
import com.spsoft.vms.redis.service.VmsQueueService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 上传发票队列服务
 * 需要上传的发票，把发票id放入队列
 * @author cdl
 * @date 2024/03/13
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UploadInvoiceVmsQueueServiceImpl implements VmsQueueService<Long> {

    private final RedissonClient redisson;

    @Override
    public boolean addMessageToQueue(Long message) {
        RQueue<Long> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD);
        if(queue.contains(message)) {
            return true;
        }
        return queue.offer(message);
    }

    @Override
    public Long getMessageFromQueue() {
        RQueue<Long> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD);
        return queue.poll();
    }

    @Override
    public List<Long> getMessagesFromQueue(int count) {
        RQueue<Long> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD);
        return queue.poll(count);
    }

    @Override
    public int getQueueSize() {
        RQueue<Long> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD);
        return queue.size();
    }

    @Override
    public boolean containsMessage(Long message) {
        RQueue<Long> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD);
        return queue.contains(message);
    }
}
